<?php
$baseUrl = Yii::app()->baseUrl;  
function getPaginationString($page = 1, $totalitems=100, $limit = 8, $adjacents = 1, $targetpage = "/", $pagestring = "/index.php/receipts&page=",$margin=5,$padding=5)
{		
	//defaults
	if(!$adjacents) $adjacents = 1;
	if(!$limit) $limit = 15;
	if(!$page) $page = 1;
	if(!$targetpage) $targetpage = "/";
	
	//other vars
	$prev = $page - 1;									//previous page is page - 1
	$next = $page + 1;									//next page is page + 1
	$lastpage = ceil($totalitems / $limit);				//lastpage is = total items / items per page, rounded up.
	$lpm1 = $lastpage - 1;								//last page minus 1
	
	/* 
		Now we apply our rules and draw the pagination object. 
		We're actually saving the code to a variable in case we want to draw it more than once.
	*/
	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<div class=\"pagination\"";
		if($margin || $padding)
		{
			$pagination .= " style=\"";
			if($margin)
				$pagination .= "margin: $margin;";
			if($padding)
				$pagination .= "padding: $padding;";
			$pagination .= "\"";
		}
		$pagination .= ">";

		//previous button
		if ($page > 1) 
			$pagination .= "<a href=\"$targetpage$pagestring$prev\">&#171; prev</a>";
		else
			$pagination .= "<span class=\"disabled\">&#171; prev</span>";	
		
		//pages	
		if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
					$pagination .= "<span class=\"current\">$counter</span>";
				else
					$pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";					
			}
		}
		elseif($lastpage >= 7 + ($adjacents * 2))	//enough pages to hide some
		{
			//close to beginning; only hide later pages
			if($page < 1 + ($adjacents * 3))		
			{
				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
				{
					if ($counter == $page)
						$pagination .= "<span class=\"current\">$counter</span>";
					else
						$pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";					
				}
				$pagination .= "<span class=\"elipses\">...</span>";
				$pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>";
				$pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>";		
			}
			//in middle; hide some front and some back
			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
			{
				$pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>";
				$pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>";
				$pagination .= "<span class=\"elipses\">...</span>";
				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
				{
					if ($counter == $page)
						$pagination .= "<span class=\"current\">$counter</span>";
					else
						$pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";					
				}
				$pagination .= "...";
				$pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>";
				$pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>";		
			}
			//close to end; only hide early pages
			else
			{
				$pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>";
				$pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>";
				$pagination .= "<span class=\"elipses\">...</span>";
				for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
						$pagination .= "<span class=\"current\">$counter</span>";
					else
						$pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";					
				}
			}
		}
		
		//next button
		if ($page < $counter - 1) 
			$pagination .= "<a href=\"" . $targetpage . $pagestring . $next . "\">next &#187;</a>";
		else
			$pagination .= "<span class=\"disabled\">next &#187;</span>";
		$pagination .= "</div>\n";
	}
	
	return $pagination;

}
?>
<div class="dialog_title">
   <a id="receipt_close" class="dialog_x">X</a>
    <h1>Attach Receipt</h1>
</div>
<div class="clear"></div>
<br/>
<div id="sideNav_wrapper">
<div id="sideNav">
<div class="sideNavFloatContainer">
<div id="search" class="section filterContainer">
<form class="form" id="filter">
<fieldset><legend>Filter</legend>
<ol>
	<li>
	<ol>
		<li><label class="dateField">From</label> <input type="text"
			class="calendar semwal_dtpicker" id="startDate"
			name="startDate" value="<?php echo date('Y-m-d')?>"></li>
		<li><label class="dateField">To</label> <input type="text"
			class="calendar semwal_dtpicker" id="endDate"
			name="endDate" value="<?php echo date('Y-m-d')?>"></li>
	</ol>
	</li>
	<li><select class="qtip" id="filter_selector" name="filter_selector">
		<option value="UNREPORTED">Unreported receipts</option>
		<option value="UNSUBMITTED" selected="selected">Unsubmitted receipts</option>
		<option value="SUBMITTED">Submitted receipts</option>
		<option value="DELETED">Deleted receipts</option>
	</select></li>
	<ul>
		<li class="label-auto"><input type="radio" checked="checked"
			value="UNATTACHED" id="unattachedFilter" class="filter-checkbox" name="attach"> <label
			for="unattachedFilter">Unattached receipts</label></li>
		<li class="label-auto"><input type="radio" 
			value="ATTACHED" id="attachedFilter" class="filter-checkbox" name="attach"> <label
			for="attachedFilter">Attached receipts</label></li>
	</ul>

</ol>
</fieldset>
</form>
</div>
<div class="section">
<div class="button">
<a id="btn_search">Search</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a id="btn_reset" class="reset" href="#">Reset</a></div>
<img  class="hide" id="search_spinner" alt="uploading" src="<?php echo $baseUrl;?>/images/spinner_2x.gif">
</div>
</div>
</div>
</div>

<div id="content_wrapper">
<div id="editheader">
<div class="buttons_container hidden" id="receipts_actions"
	style="display: block;">

<div class="floatWrapper">
<div class="floatLeft buttons_container marginBottom"
	id="receipts_buttonsContainer"><!-- Action Buttons -->
<div class="button add" id="button_importButton"><a class="dropdown" href="#">Import from</a>
</div>
<input id="file" name="file" type="file" style="visibility:hidden;" multiple >
<div class="floatLeft marginLeft" id="receipts_selectAll">
<ol class="form one-line">
	<li class="checkbox"><input type="checkbox"
		 id="receipts_selectAllCheckbox"> <label
		for="receipts_selectAllCheckbox">Select All</label></li>
</ol>
</div>
<div class="button undelete" id="receipts_batchUndelete"
	style="display: none;"><a>Undelete</a>
</div>
<div class="button delete" id="receipts_batchDelete"><a href="#" class="iconButton"></a></div>
</div>

<div id="receipts_reportActions" class="buttons_container floatRight"><!-- Report Actions -->
<div class="button add qtip" id="receipts_addReport"><img  class="hide" id="addreport_spinner" alt="uploading" src="<?php echo $baseUrl;?>/images/spinner_2x.gif"><a>Add to Report</a></div>
<div class="floatLeft">
<div class="floatLeft qtip" id="receipts_reportSelectorContainer"><select
	class="reportSelector" id="receipts_reportSelector">
	<option class="optionNone" value="0" selected="selected">(none)</option>
	<?php foreach ($reports as $report):?>
	<option value="<?php echo $report['Id']?>"><?php echo $report['ReportName']?></option>
	<?php endforeach;?>
	<option value="" class="separator" disabled="disabled">----------</option>
	<option value="1">(create new report)</option>
</select></div>
<div class="button qtip hidden" id="receipts_viewReport"
	style="display: block;"><a>View Report</a></div>
<div class="button add qtip hidden" id="receipts_newReport"
	style="display: none;"><a>Add to New Report</a></div>
</div>
</div>
</div>
</div>
</div>

<div class="page-header floatWrapper marginBottom">

<div class="hidden" id="file-uploader-page" style="display: block;">
<div class="qq-uploader">
<div class="qq-upload-drop-area" id="qq-upload-drop-area">
<div class="link" id="reports_popup_import_computer"
	style="position: relative; overflow: hidden; direction: ltr; display: none;"><a>Computer</a><input
	type="file" name="file"style="position: absolute; right: 0px; top: 0px; font-family: Arial; font-size: 118px; margin: 0px; padding: 0px; cursor: pointer; opacity: 0;"></div>
<span><strong>Quick Import:</strong> Drag and drop receipts here from
your computer.</span></div>
<ul class="qq-upload-list hidden"></ul>
 <span id="spinter-uploading" style="display: none;"><img  alt="uploading" src="<?php echo $baseUrl;?>/images/spinner_2x.gif">Uploading... </span>
</div>
</div>
</div>
<div class="floatWrapper" id="allReceiptsContainer">
<?php foreach ($rows as $row):?>
<div id="receipt_<?php echo $row['Id'];?>" class="receipt_wrapper">
<div class="receipt OPEN"><span class="receiptID"><img id="img_receipt_<?php echo $row['Id'];?>" src="<?php echo $row['Url'];?>"
	id="receiptimg_<?php echo $row['Id'];?>" class="receiptimage"><a
	href="<?php echo $row['Url'];?>"
	class="fancy" rel="receiptList"></a></span>
<div class="scanStatus hidden" style="display: none;"></div>
<div class="receipt_caption">
<div class="receipt_actions">
<div id="attach_receipt_button_<?php echo $row['Id']; ?>" class="button add attach_receipt_button"><a>Attach</a></div>
<div class="button delete delete_receipt_button"><a class="iconButton"
	href="#"></a></div>
</div>
</div>
</div>
</div>
<?php endforeach;?>
<?php echo getPaginationString($page,$totalItem,$limit,1,Yii::app()->params['domain'].'/','index.php/receipts/index/page/');?>
</div>
</div>

